package logs

import (
	

	
	
	
	
)

// DedupStrategy represents a deduplication strategy.
type DedupStrategy string

const (
	None      DedupStrategy = "none"
	Exact     DedupStrategy = "exact"
	Numbers   DedupStrategy = "numbers"
	Signature DedupStrategy = "signature"
)

// SortOrder represents a sort order.
type SortOrder string

const (
	Asc  SortOrder = "Ascending"
	Desc SortOrder = "Descending"
)

// Option represents an option that can be used to configure a logs panel.
type Option func(logs *Logs) error

// Logs represents a logs panel.
type Logs struct {
	Builder *sdk.Panel
}

// New creates a new logs panel.
func ( string,  ...Option) (*Logs, error) {
	 := &Logs{Builder: sdk.NewLogs()}
	.Builder.IsNew = false
	.Builder.LogsPanel.Options.EnableLogDetails = true

	for ,  := range append(defaults(), ...) {
		if  := ();  != nil {
			return nil, 
		}
	}

	return , nil
}

func defaults() []Option {
	return []Option{
		Span(6),
		Order(Desc),
	}
}

// Links adds links to be displayed on this panel.
func ( ...links.Link) Option {
	return func( *Logs) error {
		.Builder.Links = make([]sdk.Link, 0, len())

		for ,  := range  {
			.Builder.Links = append(.Builder.Links, .Builder)
		}

		return nil
	}
}

// DataSource sets the data source to be used by the panel.
func ( string) Option {
	return func( *Logs) error {
		.Builder.Datasource = &sdk.DatasourceRef{LegacyName: }

		return nil
	}
}

// WithLokiTarget adds a loki query to the graph.
func ( string,  ...loki.Option) Option {
	 := loki.New(, ...)

	return func( *Logs) error {
		.Builder.AddTarget(&sdk.Target{
			RefID:        .Ref,
			Expr:         .Expr,
			LegendFormat: .LegendFormat,
		})

		return nil
	}
}

// Span sets the width of the panel, in grid units. Should be a positive
// number between 1 and 12. Example: 6.
func ( float32) Option {
	return func( *Logs) error {
		if  < 1 ||  > 12 {
			return fmt.Errorf("span must be between 1 and 12: %w", errors.ErrInvalidArgument)
		}

		.Builder.Span = 

		return nil
	}
}

// Height sets the height of the panel, in pixels. Example: "400px".
func ( string) Option {
	return func( *Logs) error {
		.Builder.Height = &

		return nil
	}
}

// Description annotates the current visualization with a human-readable description.
func ( string) Option {
	return func( *Logs) error {
		.Builder.Description = &

		return nil
	}
}

// Transparent makes the background transparent.
func () Option {
	return func( *Logs) error {
		.Builder.Transparent = true

		return nil
	}
}

// Repeat configures repeating a panel for a variable
func ( string) Option {
	return func( *Logs) error {
		.Builder.Repeat = &

		return nil
	}
}

// RepeatDirection configures repeating vertical or horizontal
func ( sdk.RepeatDirection) Option {
	return func( *Logs) error {
		.Builder.RepeatDirection = &

		return nil
	}
}

// Time displays the "time" column. This is the timestamp associated with the
// log line as reported from the data source.
func () Option {
	return func( *Logs) error {
		.Builder.LogsPanel.Options.ShowTime = true

		return nil
	}
}

// UniqueLabels displays the "unique labels" column, which shows only non-common labels.
func () Option {
	return func( *Logs) error {
		.Builder.LogsPanel.Options.ShowLabels = true

		return nil
	}
}

// CommonLabels displays the "common labels".
func () Option {
	return func( *Logs) error {
		.Builder.LogsPanel.Options.ShowCommonLabels = true

		return nil
	}
}

// WrapLines enables line wrapping.
func () Option {
	return func( *Logs) error {
		.Builder.LogsPanel.Options.WrapLogMessage = true

		return nil
	}
}

// PrettifyJSON pretty prints all JSON logs. This setting does not affect logs
// in any format other than JSON.
func () Option {
	return func( *Logs) error {
		.Builder.LogsPanel.Options.PrettifyLogMessage = true

		return nil
	}
}

// HideLogDetails disables the log details view for each log row.
func () Option {
	return func( *Logs) error {
		.Builder.LogsPanel.Options.EnableLogDetails = false

		return nil
	}
}

// Order display results in descending or ascending time order.
// The default is Descending, showing the newest logs first.
// Set to Ascending to show the oldest log lines first.
func ( SortOrder) Option {
	return func( *Logs) error {
		.Builder.LogsPanel.Options.SortOrder = string()

		return nil
	}
}

// Deduplication sets the deduplication strategy.
func ( DedupStrategy) Option {
	return func( *Logs) error {
		.Builder.LogsPanel.Options.DedupStrategy = string()

		return nil
	}
}